import pyautogui
import pydirectinput
import time
import ctypes, sys
import aircv as ac
import numpy as np
import os
import pyperclip

MOUSE_POS = [[0] * 2 for _ in range(11)]  # 坐标点组

firstOpenMap = 1 # 第一次打开地图，地图最小化
remaining = 7 # 剩余押镖次数
escortPlace = 2 # 0:东岳；1:杭州；2:开封；3:徐海
escortType = 0 # 0:银两镖；1:碎银镖
role = 0 # 0:队长；1:队员
flyOrFoot = 1 # 0:使用花钱传送；1:不使用花钱传送（不包含第一次去）
temp_XY = (0, 0)
running_log = open("log.txt","w")

choice_UI = {"国服":("血条颜色","传送颜色"),"国际服":("血条颜色","传送颜色")}
choice_addr = {0:-45,1:-20,2:10,3:40} # 0:东岳；1:杭州；2:开封；3:徐海
choice_dongyue = {0:(1486,2479), 1:(591,3168), 2:(584,3174)}
choice_hangzhou = {0:(1173,2455), 1:(2243,2651), 2:(2235,2652)}
choice_kaifeng = {0:(1842,488), 1:(1038,1428), 2:(1030,1424)}
choice_xuhai = {0:(1582,2405), 1:(2338,1720), 2:(2339,1712)}
choice_place = {0:choice_dongyue, 1:choice_hangzhou, 2:choice_kaifeng, 3:choice_xuhai}

# 判断是否为管理员权限
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

# 寻找游戏窗口
def findWindow():
    windows = pyautogui.getWindowsWithTitle('天涯明月刀')
    if len(windows) == 0:
        raise Exception("窗口未找到")
    return windows[0]

# 移动鼠标（X坐标，Y坐标，移动时间）
def moveMouseTo(x, y):
    pydirectinput.moveTo(x, y)


# 移动鼠标并点击1次（X坐标，Y坐标，移动时间，左键/右键）
def moveMouseAndClick(x, y):
    moveMouseTo(x, y)
    pydirectinput.click()


def matchImg(filePath):
    screenshotIm = pyautogui.screenshot()
    imgsrc = np.array(screenshotIm)  # 打开查找页(全屏)
    imgobj = ac.imread(filePath)  # 打开待识别的图片
    match_result = ac.find_all_template(imgsrc, imgobj, 0.9)  # 0.9是识别达标率
    if(len(match_result) != 0):
        global temp_XY
        temp_XY = match_result[0]['result']
        running_log.write('找到图片中心坐标: ( ' + str(temp_XY[0]) + ', ' + str(temp_XY[1]) + ')')
        return True
    else:
       running_log.write('未找到。。。')
       return False

def shoutAuto():
    # 脚本侧式中，请勿劫镖，感谢
    pydirectinput.press("enter")
    pydirectinput.keyDown('ctrl')
    pydirectinput.keyDown('v')
    pydirectinput.keyUp('v')
    pydirectinput.keyUp('ctrl')
    pydirectinput.press("space")

    # 将字符串写入剪贴板

def addToClipBoard(text):
    command = 'echo ' + text.strip() + '| clip'
    os.system(command)

def autoRide():
    time.sleep(0.5)
    pydirectinput.keyDown("ctrl")
    pydirectinput.keyDown("r")
    pydirectinput.keyUp("r")
    pydirectinput.keyUp("ctrl")


# 是否到达NPC附近
def IsArriveNPC():
    pydirectinput.press('m')
    print('1')
    time.sleep(5)
    if(matchImg('img/copy.png')):
        print('2')
        time.sleep(5)
        moveMouseAndClick(int(temp_XY[0]),int(temp_XY[1])) # 点击复制
        print('3')
        time.sleep(5)
        str = pyperclip.paste()
        print(str) # 杭州(2244,2653)
        print('到达NPC附近')



if __name__ == "__main__":
    gameWindow = findWindow()
    gameWindow.activate()  # 激活窗口,将窗口最前化
    running_log.write("开始执行...\n")
    # addToClipBoard('脚本侧式中，请勿劫镖，感谢')

    if is_admin():
      
        IsArriveNPC()   
        while(1):
            time.sleep(100)
    else:
        ctypes.windll.shell32.ShellExecuteW(None,"runas",sys.executable,__file__,None,1)

